﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord;
using Mang24k99.Model.COMM;
using Castle.ActiveRecord.Queries;

namespace Mang24k99.Model.GOLD
{
    [ActiveRecord(Table = "KT_GOLD_PRODUCT")]
    public partial class MKtGoldProduct : ActiveRecordBase
    {
        public static IList<MKtGoldProduct> SelectAll()
        {
            return (IList<MKtGoldProduct>)FindAll(typeof(MKtGoldProduct));
        }
        public static MKtGoldProduct SelectByPrimaryKey(object ID)
        {
            return (MKtGoldProduct)FindByPrimaryKey(typeof(MKtGoldProduct), ID);
        }
        public static IList<MKtGoldProduct> SelectALLByProperty(string property, object value)
        {
            return (IList<MKtGoldProduct>)FindAllByProperty(typeof(MKtGoldProduct), property, value);
        }
        public static IList<MKtGoldProduct> SelectALLSliced(int firstResult, int maxResult, NHibernate.Expression.DetachedCriteria criteria)
        {

            return (IList<MKtGoldProduct>)SlicedFindAll(typeof(MKtGoldProduct), firstResult, maxResult, criteria);
        }
        protected override void OnDelete()
        {
            MKtGoldProductHistory mHistory = new MKtGoldProductHistory();
            Common.ModelCommon.CopyObjectValues(this, mHistory);
            mHistory.OrgnizeNO = MKtCommOrglist.OrgnizeNO;
            mHistory.BreedNO = MKtGoldGoldbreed.BreedNO;
            mHistory.StandardNO = MKtCommCodelist.CODE_NO;
            mHistory.DeleteTime = DateTime.Now;
            mHistory.Create();
            base.OnDelete();
        }
        /// <summary>
        /// 创建一条记录
        /// </summary>
        /// <param name="goldproduct"></param>
        public static void Create(MKtGoldProduct goldproduct)
        {
            goldproduct.Create();
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="goldproduct"></param>
        public static void Update(MKtGoldProduct goldproduct)
        {
            goldproduct.Update();
        }
        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="goldproduct"></param>
        public static void Delete(MKtGoldProduct goldproduct)
        {
            goldproduct.Delete();
        }
        /// <summary>
        /// 选择最大的品种编号
        /// </summary>
        /// <returns></returns>
        public static int GetMaxProductNO()
        {
            string Hql = @"select max(goldproduct.ProcductNO) from MKtGoldProduct goldproduct  ";
            string count = @"select count(goldproduct.ProcductNO) from MKtGoldProduct goldproduct ";
            ScalarQuery querycount = new ScalarQuery(typeof(MKtGoldProduct), count);
            if (Convert.ToInt32(ExecuteQuery(querycount)) == 0)
            {
                return 0;
            }
            ScalarQuery query = new ScalarQuery(typeof(MKtGoldProduct), Hql);
            return (int)ExecuteQuery(query);
        }

        /// <summary>
        /// 根据机构编号，金品品种号选择最大的内页排序号
        /// </summary>
        /// <returns></returns>
        public static int GetMaxSortNO(int orglist,int goldtype)
        {
            string Hql = @"select max(goldproduct.PageOrder) from MKtGoldProduct goldproduct ";
                    Hql += " where goldproduct.MKtCommOrglist.OrgnizeNO="+orglist;
                    Hql += " and goldproduct.MKtGoldGoldbreed.MKtCommCodelist.CODE_NO=" + goldtype;
            string count = @"select count(goldproduct.PageOrder) from MKtGoldProduct goldproduct ";
                    count += " where goldproduct.MKtCommOrglist.OrgnizeNO=" + orglist;
                    count += " and goldproduct.MKtGoldGoldbreed.MKtCommCodelist.CODE_NO=" + goldtype;
            ScalarQuery querycount = new ScalarQuery(typeof(MKtGoldProduct), count);
            if (Convert.ToInt32(ExecuteQuery(querycount)) == 0)
            {
                return 0;
            }
            ScalarQuery query = new ScalarQuery(typeof(MKtGoldProduct), Hql);
            return (int)ExecuteQuery(query);
        }
        /// <summary>
        /// 选择需在首页排序的产品的最大首页排序号
        /// </summary>
        /// <returns></returns>
        public static int GetMaxMpageSortNO()
        {
            MKtGoldProduct goldproduct = new MKtGoldProduct();
            string Hql = @"select max(goldproduct.MpageOrder) from MKtGoldProduct goldproduct ";
            Hql += " where goldproduct.MKtCommOrglist.ParentNO=3";
            Hql += " and goldproduct.CloseShow=0";
            string count = @"select count(goldproduct.MpageOrder) from MKtGoldProduct goldproduct ";
            count += " where goldproduct.MKtCommOrglist.ParentNO=3";
            count += " and goldproduct.CloseShow=0";
            ScalarQuery querycount = new ScalarQuery(typeof(MKtGoldProduct), count);
            if (Convert.ToInt32(ExecuteQuery(querycount)) == 0)
            {
                return 0;
            }
            ScalarQuery query = new ScalarQuery(typeof(MKtGoldProduct), Hql);
            return (int)ExecuteQuery(query);

        }

        public static IList<MKtGoldProduct> SelectBywhere(string where)
        {
            string hql = @"from MKtGoldProduct goldproduct "+where;
            SimpleQuery query = new SimpleQuery(typeof(MKtGoldProduct),hql);
            return (IList<MKtGoldProduct>)ExecuteQuery(query);
            
        }
        /// <summary>
        /// 一对一关系
        /// </summary>
        [OneToOne(Cascade = CascadeEnum.All)]
        public MKtGoldSaleprice   MKtGoldSaleprice
        {
            get
            {
                if (_MKtGoldSaleprice==null)
                {
                    _MKtGoldSaleprice = new MKtGoldSaleprice();
                }
                return _MKtGoldSaleprice;
            }
            set
            {
                _MKtGoldSaleprice = value;
            }
        }

        [BelongsTo("ORGNIZE_NO", Cascade = CascadeEnum.None)]
        public MKtCommOrglist MKtCommOrglist
        {
            get
            {
                return _MKtCommOrglist;
            }
            set
            {
                _MKtCommOrglist = value;
            }
        }
        [BelongsTo("BREED_NO", Cascade = CascadeEnum.None)]
        public MKtGoldGoldbreed MKtGoldGoldbreed
        {
            get
            {
                return _MKtGoldGoldbreed;
            }
            set
            {
                _MKtGoldGoldbreed = value;
            }
        }
        [BelongsTo("STANDARD_NO", Cascade = CascadeEnum.None)]
        public MKtCommCodeList  MKtCommCodelist
        {
            get
            {
                return _MKtCommCodelist;
            }
            set
            {
                _MKtCommCodelist = value;
            }
        }

        /// <summary>
        /// Gets or sets the ProcductNO.()
        /// </summary>
        /// <value>The ProcductNO.</value>
        [PrimaryKey(PrimaryKeyType.Assigned, "PROCDUCT_NO")]
        public int ProcductNO
        {
            get
            {
                return _ProcductNO;
            }
            set
            {
                _ProcductNO = value;
            }
        }


        /// <summary>
        /// Gets or sets the ID.()
        /// </summary>
        /// <value>The ID.</value>
        [Property("ID", Insert = false, Update = false)]
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }


        /// <summary>
        /// Gets or sets the ColumnNO.()
        /// </summary>
        /// <value>The ColumnNO.</value>
        [Property("COLUMN_NO")]
        public int? ColumnNO
        {
            get
            {
                return _ColumnNO;
            }
            set
            {
                _ColumnNO = value;
            }
        }


        /// <summary>
        /// Gets or sets the ProductName.()
        /// </summary>
        /// <value>The ProductName.</value>
        [Property("PRODUCT_NAME")]
        public string ProductName
        {
            get
            {
                return _ProductName;
            }
            set
            {
                _ProductName = value;
            }
        }


        /// <summary>
        /// Gets or sets the MEASUREMENT.()
        /// </summary>
        /// <value>The MEASUREMENT.</value>
        [Property("MEASUREMENT")]
        public int? MEASUREMENT
        {
            get
            {
                return _MEASUREMENT;
            }
            set
            {
                _MEASUREMENT = value;
            }
        }


        /// <summary>
        /// Gets or sets the ProductBelong.()
        /// </summary>
        /// <value>The ProductBelong.</value>
        [Property("PRODUCT_BELONG")]
        public int? ProductBelong
        {
            get
            {
                return _ProductBelong;
            }
            set
            {
                _ProductBelong = value;
            }
        }


        /// <summary>
        /// Gets or sets the PageOrder.()
        /// </summary>
        /// <value>The PageOrder.</value>
        [Property("PAGE_ORDER")]
        public int? PageOrder
        {
            get
            {
                return _PageOrder;
            }
            set
            {
                _PageOrder = value;
            }
        }


        /// <summary>
        /// Gets or sets the MpageOrder.()
        /// </summary>
        /// <value>The MpageOrder.</value>
        [Property("MPAGE_ORDER")]
        public int? MpageOrder
        {
            get
            {
                return _MpageOrder;
            }
            set
            {
                _MpageOrder = value;
            }
        }


        /// <summary>
        /// Gets or sets the CloseShow.()
        /// </summary>
        /// <value>The CloseShow.</value>
        [Property("CLOSE_SHOW")]
        public int? CloseShow
        {
            get
            {
                return _CloseShow;
            }
            set
            {
                _CloseShow = value;
            }
        }
        /// <summary>
        /// 是否首页排序
        /// </summary>
        [Property("IS_MPORDER")]
        public int? Is_Mporder
        {
            get
            {
                return _Is_Mporder;
            }
            set
            {
                _Is_Mporder = value;
            }
        }

        /// <summary>
        /// Gets or sets the IsValidate.()
        /// </summary>
        /// <value>The IsValidate.</value>
        [Property("IS_VALIDATE")]
        public int? IsValidate
        {
            get
            {
                return _IsValidate;
            }
            set
            {
                _IsValidate = value;
            }
        }


        /// <summary>
        /// Gets or sets the InputTime.()
        /// </summary>
        /// <value>The InputTime.</value>
        [Property("INPUT_TIME")]
        public DateTime? InputTime
        {
            get
            {
                return _InputTime;
            }
            set
            {
                _InputTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the ModifyTime.()
        /// </summary>
        /// <value>The ModifyTime.</value>
        [Property("MODIFY_TIME")]
        public DateTime? ModifyTime
        {
            get
            {
                return _ModifyTime;
            }
            set
            {
                _ModifyTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the DeleteTime.()
        /// </summary>
        /// <value>The DeleteTime.</value>
        [Property("DELETE_TIME")]
        public DateTime? DeleteTime
        {
            get
            {
                return _DeleteTime;
            }
            set
            {
                _DeleteTime = value;
            }
        }



        private int _ProcductNO;

        private int _ID;
        private MKtGoldSaleprice _MKtGoldSaleprice;
        private MKtCommOrglist _MKtCommOrglist;
        private int? _ColumnNO;

        private string _ProductName;

        private MKtGoldGoldbreed _MKtGoldGoldbreed;
        private MKtCommCodeList _MKtCommCodelist;
        private int? _MEASUREMENT;

        private int? _ProductBelong;

        private int? _PageOrder;

        private int? _MpageOrder;

        private int? _CloseShow;
        private int? _Is_Mporder;
        private int? _IsValidate;

        private DateTime? _InputTime;

        private DateTime? _ModifyTime;

        private DateTime? _DeleteTime;


    }
}
